package com.weitu.observation.observationCard.controller;


import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.constant.DictionaryTypeDefConstants;
import com.weitu.base.controller.BaseController;
import com.weitu.majorHazard.technicalReport.entity.TechnicalReport;
import com.weitu.observation.observationCard.entity.ObservationCard;
import com.weitu.observation.observationCard.entity.ObservationCardOpinionCreate;
import com.weitu.observation.observationCard.entity.ObservationCardOptionAnalysis;
import com.weitu.observation.observationCard.service.ObservationCardService;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.dictionary.entity.Dictionary;
import com.weitu.permission.user.entity.User;
import com.weitu.permission.user.service.UserService;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import com.weitu.workflow.definition.constant.FlowDefConstants;
import com.weitu.workflow.flowBusiness.constant.FlowBusinessConstants;
import com.weitu.workflow.flowManagement.entity.WorkflowReview;
import com.weitu.workflow.flowManagement.service.FlowManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/4/1.
 */
@Controller
@RequestMapping(value = "observation/observationCard")
public class ObservationCardController extends BaseController {
    private static final String MODULE_BATH_PATH="/WEB-INF/jsp/observation/observationCard";

    private final ObservationCardService observationCardService;
    private final FlowManagementService flowManagementService;
    private final UserService userService;
    @Autowired
    public ObservationCardController(ObservationCardService observationCardService,FlowManagementService flowManagementService,UserService userService){
        this.observationCardService=observationCardService;
        this.flowManagementService=flowManagementService;
        this.userService=userService;
    }

    /**
     * 调到观察卡列表页面
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return MODULE_BATH_PATH+"/list";
    }

    /**
     * 观察卡列表页面数据
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request){

        Map<String,Object> paramMap=new HashMap<String,Object>();
        putParametersIntoMap(paramMap);
        int count=this.observationCardService.queryCount(paramMap);

        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,count);

        PageView pageView=dataGridPagination.getPageView();
        paramMap.put("pageView",pageView);

        List<ObservationCard> dataList=this.observationCardService.queryObservationList(paramMap);
        return dataGridPagination.flushDataGridData(dataList);
    }

    /**
     * 跳往添加页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public ModelAndView addObservationCard(){
        ModelAndView modelAndView=new ModelAndView();
        List<Dictionary> dictionaryList1=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PEOPLE_REACTION);
        List<Dictionary> dictionaryList2=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PROTECT_EQUIPMENT);
        List<Dictionary> dictionaryList3=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PEOPLE_PLACE);
        List<Dictionary> dictionaryList4=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.TOOLS_EQUIPMENT);
        List<List> list=new ArrayList<List>();
        list.add(dictionaryList1);
        list.add(dictionaryList2);
        list.add(dictionaryList3);
        list.add(dictionaryList4);
        modelAndView.addObject("list",list);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("userId",BaseController.getSessionUser().getId());
        map.put("userName",BaseController.getSessionUser().getName());
        map.put("userRealName",BaseController.getSessionUser().getRealName());
        map.put("deptId",BaseController.getSessionUser().getDepartment().getId());
        map.put("deptName",BaseController.getSessionUser().getDepartment().getName());
        modelAndView.addObject("userMessage",map);
        modelAndView.setViewName(MODULE_BATH_PATH+"/add");
        return modelAndView;
    }

    /**
     * 添加
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @SystemLog(module = "观察卡登记", methods = "添加观察卡")
    public AjaxResult addObservationCardAjax(ObservationCard observationCard,@RequestParam(value = "dictionaryId",required = false) String[] params) throws Exception{
        String id = IDGenerator.getId();
        String addTime = DateTimeUtil.formatterDateTime();
        observationCard.setId(id);
        observationCard.setAddTime(addTime);
        observationCard.setDelState(0);
        observationCard.setAddDeptId(BaseController.getSessionUser().getDepartment().getId());
        observationCard.setAddUserId(BaseController.getSessionUser().getId());

        if(observationCard.getPeopleReaction()==null){
            observationCard.setPeopleReaction(0);
        }
        if(observationCard.getPeoplePlace()==null){
            observationCard.setPeoplePlace(0);
        }
        if(observationCard.getPeopleProtect()==null){
            observationCard.setPeopleProtect(0);
        }
        if(observationCard.getTool()==null){
            observationCard.setTool(0);
        }

        List<ObservationCardOpinionCreate> list =new ArrayList<ObservationCardOpinionCreate>();
        if(params!=null&&params.length>0)
        for(String param:params){
            ObservationCardOpinionCreate opinionCreate=new ObservationCardOpinionCreate();
            opinionCreate.setCardId(id);
            opinionCreate.setOpinionId(param);
            list.add(opinionCreate);
        }
        int count=this.observationCardService.insertCard(observationCard,list);





        if(observationCard.getObsStatus().equals("等待分析")){
            User loginUser=getSessionUser();

            // Map<String, Object> paramMap = new HashMap<String, Object>();
            //获取前台参数
            // getEditBasicFieldMap(paramMap);

            //String id=paramMap.get("id").toString();//id
            //设置流程变量
            Map<String,String> dataMap=new HashMap<String, String>();
            //String nextUserId=paramMap.get("nextUserId").toString();//第一环节处理人
            String nextUserId=this.userService.selectById(observationCard.getAnalysisUser()).getName();//第一环节处理人
            dataMap.put(FlowDefConstants.FLOW_FIRSTUSER_ID,nextUserId);
            String content="观察卡分析处理流程";
            //启动流程实例
            flowManagementService.flowCreateAndRun(FlowBusinessConstants.BUSINESS_OBS_CARD,id,content,loginUser.getName(),dataMap);
        }



        if(count==1){

            return AjaxResult.success("保存成功");
        }else{

            return AjaxResult.fail("保存失败！");
        }
    }

    /**
     * 跳转编辑页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/editCard",method = RequestMethod.GET)
    public ModelAndView editCard(String id){
        if(null==id){
            Map<String, Object> paramMap = new HashMap<String, Object>();
            //获取参数
            putParametersIntoMap(paramMap);
            id=paramMap.get("businessId").toString();
        }
        ObservationCard observationCard=this.observationCardService.selectById(id);
        ModelAndView modelAndView=new ModelAndView();
        /**
         * 添加人观察提示
         */
        List<Dictionary> dictionaryList1=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PEOPLE_REACTION);
        List<Dictionary> dictionaryList2=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PROTECT_EQUIPMENT);
        List<Dictionary> dictionaryList3=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PEOPLE_PLACE);
        List<Dictionary> dictionaryList4=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.TOOLS_EQUIPMENT);
        List<List<Dictionary>> list=new ArrayList<List<Dictionary>>();
        list.add(dictionaryList1);
        list.add(dictionaryList2);
        list.add(dictionaryList3);
        list.add(dictionaryList4);
        List<ObservationCardOpinionCreate> opinionCreateList=this.observationCardService.selectOpinionIdByCardId(id);
        modelAndView.addObject("list",list);
        modelAndView.addObject("opinionCreateList",opinionCreateList);
        modelAndView.addObject("card",observationCard);
        if(observationCard.getObsStatus().equals("已登记")){
            if(observationCard.getObsReporter().equals(BaseController.getSessionUser().getId())){
                modelAndView.addObject("auth","yes");
            }else{
                modelAndView.addObject("auth","no");
            }
            modelAndView.setViewName(MODULE_BATH_PATH+"/edit");
            return modelAndView;
        }
        /**
         * 分析人分析提示
         */
            List<Dictionary> dictionaryList6=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.UNSAFE_STATE);
            List<Dictionary> dictionaryList5=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.UNSAFE_ACT);
            List<Dictionary> dictionaryList7=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.INDIRECT_CAUSE);
            List<List<Dictionary>> listAnalysis=new ArrayList<List<Dictionary>>();
            listAnalysis.add(dictionaryList5);
            listAnalysis.add(dictionaryList6);
            listAnalysis.add(dictionaryList7);
            modelAndView.addObject("listAnalysis",listAnalysis);

        List<ObservationCardOptionAnalysis> optionAnalysisList=this.observationCardService.selectOptionAnalysisIdByCardId(id);
        if(observationCard.getObsStatus().equals("等待分析")){
//            if(observationCard.getAnalysisUser().equals(BaseController.getSessionUser().getId())){
//                modelAndView.addObject("auth","no");
//            }else{
                modelAndView.addObject("auth","no");
//            }
            modelAndView.addObject("optionAnalysisList",optionAnalysisList);
            modelAndView.setViewName(MODULE_BATH_PATH+"/analysis");
            return modelAndView;
        }
        if(observationCard.getObsStatus().equals("分析中")){
            modelAndView.setViewName(MODULE_BATH_PATH+"/analysising");
            return modelAndView;
        }
        if(observationCard.getObsStatus().equals("等待处理")){
//            if(observationCard.getHandleUser().equals(BaseController.getSessionUser().getId())){
//                modelAndView.addObject("auth","no");
//            }else{
                modelAndView.addObject("auth","no");
//            }
            modelAndView.addObject("optionAnalysisList",optionAnalysisList);
            modelAndView.setViewName(MODULE_BATH_PATH+"/handle");
            return modelAndView;
        }
        if(observationCard.getObsStatus().equals("处理完毕")){
            modelAndView.addObject("optionAnalysisList",optionAnalysisList);
            modelAndView.setViewName(MODULE_BATH_PATH+"/handleFinash");
            return modelAndView;
        }
        if(observationCard.getObsStatus().equals("未处理完毕")){
            modelAndView.addObject("optionAnalysisList",optionAnalysisList);
            modelAndView.setViewName(MODULE_BATH_PATH+"/notHandleFinash");
            return modelAndView;
        }
        return modelAndView;
    }

    /**
     * 跳转编辑页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/detailCard",method = RequestMethod.GET)
    public ModelAndView detailCard(String id){
        ObservationCard observationCard=this.observationCardService.selectById(id);
        ModelAndView modelAndView=new ModelAndView();
        /**
         * 添加人观察提示
         */
        List<Dictionary> dictionaryList1=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PEOPLE_REACTION);
        List<Dictionary> dictionaryList2=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PROTECT_EQUIPMENT);
        List<Dictionary> dictionaryList3=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PEOPLE_PLACE);
        List<Dictionary> dictionaryList4=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.TOOLS_EQUIPMENT);
        List<List<Dictionary>> list=new ArrayList<List<Dictionary>>();
        list.add(dictionaryList1);
        list.add(dictionaryList2);
        list.add(dictionaryList3);
        list.add(dictionaryList4);
        List<ObservationCardOpinionCreate> opinionCreateList=this.observationCardService.selectOpinionIdByCardId(id);
        modelAndView.addObject("list",list);
        modelAndView.addObject("opinionCreateList",opinionCreateList);
        modelAndView.addObject("card",observationCard);
        if(observationCard.getObsStatus().equals("已登记")){
            if(observationCard.getObsReporter().equals(BaseController.getSessionUser().getId())){
                modelAndView.addObject("auth","no");
            }else{
                modelAndView.addObject("auth","no");
            }
            modelAndView.setViewName(MODULE_BATH_PATH+"/edit");
            return modelAndView;
        }
        /**
         * 分析人分析提示
         */
        List<Dictionary> dictionaryList6=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.UNSAFE_STATE);
        List<Dictionary> dictionaryList5=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.UNSAFE_ACT);
        List<Dictionary> dictionaryList7=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.INDIRECT_CAUSE);
        List<List<Dictionary>> listAnalysis=new ArrayList<List<Dictionary>>();
        listAnalysis.add(dictionaryList5);
        listAnalysis.add(dictionaryList6);
        listAnalysis.add(dictionaryList7);
        modelAndView.addObject("listAnalysis",listAnalysis);

        List<ObservationCardOptionAnalysis> optionAnalysisList=this.observationCardService.selectOptionAnalysisIdByCardId(id);
        if(observationCard.getObsStatus().equals("等待分析")){
//            if(observationCard.getAnalysisUser().equals(BaseController.getSessionUser().getId())){
//                modelAndView.addObject("auth","no");
//            }else{
                modelAndView.addObject("auth","no");
//            }
            modelAndView.addObject("optionAnalysisList",optionAnalysisList);
            modelAndView.setViewName(MODULE_BATH_PATH+"/analysis");
            return modelAndView;
        }
        if(observationCard.getObsStatus().equals("分析中")){
            modelAndView.setViewName(MODULE_BATH_PATH+"/analysising");
            return modelAndView;
        }
        if(observationCard.getObsStatus().equals("等待处理")){
//            if(observationCard.getHandleUser().equals(BaseController.getSessionUser().getId())){
//                modelAndView.addObject("auth","no");
//            }else{
                modelAndView.addObject("auth","no");
//            }
            modelAndView.addObject("optionAnalysisList",optionAnalysisList);
            modelAndView.setViewName(MODULE_BATH_PATH+"/handle");
            return modelAndView;
        }
        if(observationCard.getObsStatus().equals("处理完毕")){
            modelAndView.addObject("optionAnalysisList",optionAnalysisList);
            modelAndView.setViewName(MODULE_BATH_PATH+"/handleFinash");
            return modelAndView;
        }
        if(observationCard.getObsStatus().equals("未处理完毕")){
            modelAndView.addObject("optionAnalysisList",optionAnalysisList);
            modelAndView.setViewName(MODULE_BATH_PATH+"/notHandleFinash");
            return modelAndView;
        }
        return modelAndView;
    }

    /**
     * 修改观察卡
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editCard",method = RequestMethod.POST)
    @SystemLog(module = "观察卡登记",methods = "编辑观察卡")
    public AjaxResult editCardAjax(ObservationCard card,@RequestParam(value = "dictionaryId",required = false) String[] params) throws Exception{
        card.setUpdateUserId(BaseController.getSessionUser().getId());
        card.setUpdateDeptId(BaseController.getSessionUser().getDepartment().getId());
        card.setUpdateTime(DateTimeUtil.formatterDateTime());

        if(card.getPeopleReaction()==null){
            card.setPeopleReaction(0);
        }
        if(card.getPeoplePlace()==null){
            card.setPeoplePlace(0);
        }
        if(card.getPeopleProtect()==null){
            card.setPeopleProtect(0);
        }
        if(card.getTool()==null){
            card.setTool(0);
        }


        if(card.getObsStatus().equals("等待分析")){
            User loginUser=getSessionUser();

            // Map<String, Object> paramMap = new HashMap<String, Object>();
            //获取前台参数
            // getEditBasicFieldMap(paramMap);

            String id=card.getId();//id
            //设置流程变量
            Map<String,String> dataMap=new HashMap<String, String>();
            //String nextUserId=paramMap.get("nextUserId").toString();//第一环节处理人
            String nextUserId=this.userService.selectById(card.getAnalysisUser()).getName();//第一环节处理人
            dataMap.put(FlowDefConstants.FLOW_FIRSTUSER_ID,nextUserId);
            String content="观察卡分析处理流程";
            //启动流程实例
            flowManagementService.flowCreateAndRun(FlowBusinessConstants.BUSINESS_OBS_CARD,id,content,loginUser.getName(),dataMap);
        }










        List<ObservationCardOpinionCreate> list =new ArrayList<ObservationCardOpinionCreate>();
        if(params!=null&&params.length>0)
        for(String param:params){
            ObservationCardOpinionCreate opinionCreate=new ObservationCardOpinionCreate();
            opinionCreate.setCardId(card.getId());
            opinionCreate.setOpinionId(param);
            list.add(opinionCreate);
        }
        int count=this.observationCardService.updateCard(card,list,card.getId());
        if(count==1){

            return AjaxResult.success("保存成功");
        }else{

            return AjaxResult.fail("保存失败！");
        }
    }
    /**
     * 分析观察卡
     * @return
     */
    @SystemLog(module = "观察卡登记",methods = "分析观察卡")
    @ResponseBody
    @RequestMapping(value = "/analysisCard",method = RequestMethod.POST)
    public AjaxResult analysisCardAjax(ObservationCard card,@RequestParam(value="dictId",required = false) String[] paramDict,@RequestParam(value="isContinue",required = false) Integer isContinue){
        if(isContinue==1){
            card.setObsStatus("等待处理");
        }else{
            card.setObsStatus("处理完毕");
        }
        card.setAnalysisTime(DateTimeUtil.formatterDateTime());


        List<ObservationCardOptionAnalysis> list =new ArrayList<ObservationCardOptionAnalysis>();
        if(paramDict!=null&&paramDict.length>0)
        for(String param:paramDict){
            ObservationCardOptionAnalysis opinionCreate=new ObservationCardOptionAnalysis();
            opinionCreate.setCardId(card.getId());
            opinionCreate.setOpinionId(param);
            list.add(opinionCreate);
        }
        int count=this.observationCardService.analysisCard(card,list);
        if(count==1){

            return  AjaxResult.success("分析完成");
        }else{

            return  AjaxResult.fail("分析失败！");
        }
    }

    /**
     * 驳回
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/reject",method = RequestMethod.POST)
    @SystemLog(module = "观察卡登记",methods = "驳回")
    public AjaxResult reject(ObservationCard observationCard){
        observationCard.setObsStatus("已登记");
        int count=this.observationCardService.updateCard(observationCard);
        if(count==1){

            return AjaxResult.success("驳回成功");
        }else{

            return AjaxResult.fail("驳回失败！");
        }
    }

    /**
     * 处理
     * @return
     */
    @SystemLog(module = "观察卡登记",methods = "处理")
    @ResponseBody
    @RequestMapping(value = "handleCard",method = RequestMethod.POST)
    public AjaxResult handleCard(ObservationCard observationCard){
        observationCard.setObsStatus("处理完毕");
        observationCard.setHandleDo(1);
        int count=this.observationCardService.updateCard(observationCard);
        if(count==1){
            return AjaxResult.success("处理完成");
        }else{
            return AjaxResult.fail("未处理完成");
        }
    }

    /**
     * 删除观察卡
     * @param id
     * @return
     */
    @SystemLog(module = "观察卡登记",methods = "删除观察卡")
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public AjaxResult deleteCard(String id){
        int count =this.observationCardService.deleteCard(id);
        if(count==1){
            return AjaxResult.success("删除完成");
        }else{
            return AjaxResult.fail("删除失败");
        }
    }
    /**
     * 流程分析
     */
    @RequestMapping(value = "/workFlowAnalays",method = RequestMethod.GET)
    public ModelAndView workFlowAnalays(String id){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取参数
        putParametersIntoMap(paramMap);
        if(null==id){

            id=paramMap.get("businessId").toString();
        }
        ObservationCard observationCard=this.observationCardService.selectById(id);
        ModelAndView modelAndView=new ModelAndView();

        //工作项id
        String workItemId=paramMap.get("workItemId").toString();
        String businessId=paramMap.get("businessId").toString();
        //表单类型；0：只读表单；1：编辑表单
        int formType=paramMap.get("formType")!=null?Integer.parseInt(paramMap.get("formType").toString()):0;
        modelAndView.addObject("workItemId",workItemId);
        modelAndView.addObject("formType",formType);
        //获取意见信息
        WorkflowReview workflowReview=this.flowManagementService.queryWorkflowReviewByWorkItemId(workItemId);
        modelAndView.addObject("workflowReview",workflowReview);



        /**
         * 添加人观察提示
         */
        List<Dictionary> dictionaryList1=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PEOPLE_REACTION);
        List<Dictionary> dictionaryList2=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PROTECT_EQUIPMENT);
        List<Dictionary> dictionaryList3=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PEOPLE_PLACE);
        List<Dictionary> dictionaryList4=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.TOOLS_EQUIPMENT);
        List<List<Dictionary>> list=new ArrayList<List<Dictionary>>();
        list.add(dictionaryList1);
        list.add(dictionaryList2);
        list.add(dictionaryList3);
        list.add(dictionaryList4);
        List<ObservationCardOpinionCreate> opinionCreateList=this.observationCardService.selectOpinionIdByCardId(id);
        modelAndView.addObject("list",list);
        modelAndView.addObject("opinionCreateList",opinionCreateList);
        modelAndView.addObject("card",observationCard);
        /**
         * 分析人分析提示
         */
        List<Dictionary> dictionaryList6=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.UNSAFE_STATE);
        List<Dictionary> dictionaryList5=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.UNSAFE_ACT);
        List<Dictionary> dictionaryList7=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.INDIRECT_CAUSE);
        List<List<Dictionary>> listAnalysis=new ArrayList<List<Dictionary>>();
        listAnalysis.add(dictionaryList5);
        listAnalysis.add(dictionaryList6);
        listAnalysis.add(dictionaryList7);
        modelAndView.addObject("listAnalysis",listAnalysis);

        List<ObservationCardOptionAnalysis> optionAnalysisList=this.observationCardService.selectOptionAnalysisIdByCardId(id);
        if(observationCard.getObsStatus().equals("等待分析")){
            if(observationCard.getAnalysisUser().equals(BaseController.getSessionUser().getId())){
                modelAndView.addObject("auth","yes");
            }else{
                modelAndView.addObject("auth","no");
            }
            modelAndView.addObject("optionAnalysisList",optionAnalysisList);
            modelAndView.setViewName(MODULE_BATH_PATH+"/workFlowAnalysis");
            return modelAndView;
        }
        return modelAndView;
    }
    /**
     * 审核
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/verify", method = RequestMethod.POST)
    @SystemLog(module = "观察卡", methods = "分析")
    public AjaxResult verifyView(HttpServletRequest request,@RequestParam(value="dictId",required = false) String[] paramDict)throws Exception {

        String businessId = request.getParameter("id");
        ObservationCard observationCard=this.observationCardService.selectById(businessId);
        String workItemId=request.getParameter("workItemId");//获取工作项id
        //查询处理结果及意见
        WorkflowReview workflowReview=this.flowManagementService.getWorkflowReviewByRequest(request,"workflowReview",workItemId);
        //保存处理结果
        boolean boo=this.flowManagementService.saveWorkflowReview(workflowReview);
        //结束环节，继续流程向下执行
        if (boo){
            //更改业务中的审批状态
            String result = workflowReview.getResult();
            if("1".equals(result)){
                if("".equals(workflowReview.getNextUsers())){
                    observationCard.setObsStatus("处理完毕");
                }else{
                    observationCard.setObsStatus("等待处理");
                }
            }else{

                    observationCard.setObsStatus("处理完毕");


            }

            this.flowManagementService.workItemComplete(workItemId);
        }
        List<ObservationCardOptionAnalysis> list =new ArrayList<ObservationCardOptionAnalysis>();
        if(paramDict!=null&&paramDict.length>0)
            for(String param:paramDict){
                ObservationCardOptionAnalysis opinionCreate=new ObservationCardOptionAnalysis();
                opinionCreate.setCardId(observationCard.getId());
                opinionCreate.setOpinionId(param);
                list.add(opinionCreate);
            }
        int count=this.observationCardService.analysisCard(observationCard,list);
        if(count==1){

            return  AjaxResult.success("分析完成");
        }else{

            return  AjaxResult.fail("分析失败！");
        }
    }

    private AjaxResult updateById(ObservationCard observationCard){


        Integer result = observationCardService.updateCard(observationCard);
        if(result==1){
            return AjaxResult.success("提交成功");

        }else {
            return AjaxResult.fail("提交失败");
        }
    }



    /**
     * 流程分析
     */
    @RequestMapping(value = "/workFolwHandle",method = RequestMethod.GET)
    public ModelAndView workFolwHandle(String id){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取参数
        putParametersIntoMap(paramMap);
        if(null==id){

            id=paramMap.get("businessId").toString();
        }
        ObservationCard observationCard=this.observationCardService.selectById(id);
        ModelAndView modelAndView=new ModelAndView();

        //工作项id
        String workItemId=paramMap.get("workItemId").toString();
        String businessId=paramMap.get("businessId").toString();
        //表单类型；0：只读表单；1：编辑表单
        int formType=paramMap.get("formType")!=null?Integer.parseInt(paramMap.get("formType").toString()):0;
        modelAndView.addObject("workItemId",workItemId);
        modelAndView.addObject("formType",formType);
        //获取意见信息
        WorkflowReview workflowReview=this.flowManagementService.queryWorkflowReviewByWorkItemId(workItemId);
        modelAndView.addObject("workflowReview",workflowReview);



        /**
         * 添加人观察提示
         */
        List<Dictionary> dictionaryList1=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PEOPLE_REACTION);
        List<Dictionary> dictionaryList2=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PROTECT_EQUIPMENT);
        List<Dictionary> dictionaryList3=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.PEOPLE_PLACE);
        List<Dictionary> dictionaryList4=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.TOOLS_EQUIPMENT);
        List<List<Dictionary>> list=new ArrayList<List<Dictionary>>();
        list.add(dictionaryList1);
        list.add(dictionaryList2);
        list.add(dictionaryList3);
        list.add(dictionaryList4);
        List<ObservationCardOpinionCreate> opinionCreateList=this.observationCardService.selectOpinionIdByCardId(id);
        modelAndView.addObject("list",list);
        modelAndView.addObject("opinionCreateList",opinionCreateList);
        modelAndView.addObject("card",observationCard);
        /**
         * 分析人分析提示
         */
        List<Dictionary> dictionaryList6=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.UNSAFE_STATE);
        List<Dictionary> dictionaryList5=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.UNSAFE_ACT);
        List<Dictionary> dictionaryList7=this.observationCardService.selectCardPrompt(DictionaryTypeDefConstants.INDIRECT_CAUSE);
        List<List<Dictionary>> listAnalysis=new ArrayList<List<Dictionary>>();
        listAnalysis.add(dictionaryList5);
        listAnalysis.add(dictionaryList6);
        listAnalysis.add(dictionaryList7);
        modelAndView.addObject("listAnalysis",listAnalysis);

        List<ObservationCardOptionAnalysis> optionAnalysisList=this.observationCardService.selectOptionAnalysisIdByCardId(id);

        if(observationCard.getObsStatus().equals("等待处理")){
                modelAndView.addObject("auth","yes");
            modelAndView.addObject("optionAnalysisList",optionAnalysisList);
            modelAndView.setViewName(MODULE_BATH_PATH+"/workFolwHandle");
            return modelAndView;
        }
        return modelAndView;
    }


    /**
     * 审核
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/verifyHandle", method = RequestMethod.POST)
    @SystemLog(module = "观察卡", methods = "处理")
    public AjaxResult verifyHandle(HttpServletRequest request)throws Exception {


        String businessId = request.getParameter("id");
        ObservationCard observationCard=this.observationCardService.selectById(businessId);
        String workItemId=request.getParameter("workItemId");//获取工作项id
        //查询处理结果及意见
        WorkflowReview workflowReview=this.flowManagementService.getWorkflowReviewByRequest(request,"workflowReview",workItemId);
        //保存处理结果
        boolean boo=this.flowManagementService.saveWorkflowReview(workflowReview);
        //结束环节，继续流程向下执行
        if (boo){
            //更改业务中的审批状态


                observationCard.setObsStatus("处理完毕");



            this.flowManagementService.workItemComplete(workItemId);
        }

        return updateById(observationCard);
    }
}
